#include "threadPool.h"
int transfile(int netfd)
{
    train_t train;
    train.length = 5;
    memcpy(train.data, "file1", 5);
    send(netfd, &train, sizeof(train.length) + train.length, 0);

    int fd = open("file1", O_RDWR);
    struct stat statbuf;
    ;
    fstat(fd, &statbuf);
    train.length = sizeof(statbuf.st_size);
    memcpy(train.data, &statbuf.st_size, sizeof(statbuf.st_size));
    send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
    if (statbuf.st_size > 1024)
    {
        // 先mmap,把文件中的所有内容加载到内存中
        char *p = (char *)mmap(NULL, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        ERROR_CHECK(p, MAP_FAILED, "mmap");
        // 发送文件内容
        send(netfd, p, statbuf.st_size, MSG_NOSIGNAL); // 大火车触发零拷贝
        munmap(p, statbuf.st_size);
    }
    else {
        sendfile(netfd, fd, NULL, statbuf.st_size);
    }

    return 0;
}